<?php
class Areas_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

 
    /*
	查询子类
    */
    function GetListByParent($parentid)
    {
    	$this->load->database();
    	$sql = "SELECT * FROM areas WHERE ParentCode=?"; 

		$query = $this->db->query($sql,array($parentid));

		if ($query->num_rows() > 0)
		{
		   return $query->result_array(); 		 
		}
		
    }

    /**
    *  分页查询
    *  $pageindex页码,$pagesize页大小，$recordcount返回的总记录数
    */
    function GetListByPagination($pageindex,$pagesize,&$recordcount)
    {
        if ($pageindex<1) $pageindex = 1; 
        $pagestart = ($pageindex - 1) * $pagesize;
        $this->load->library('mysqlidb');

        $sql = "SELECT SQL_CALC_FOUND_ROWS  * FROM sm_markets LIMIT ".$pagestart.",".$pagesize.";SELECT FOUND_ROWS() ;"; 

        $result = $this->mysqlidb->querypagination($sql);
        $recordcount = $result[1][0][0];
        //print_r($sql);
        return $result[0];
    }

    /**
    * 获取实体
    */
    function GetEntity($id)
    {
        $this->load->database();
        $sql = "SELECT * FROM sm_markets WHERE id = ? LIMIT 1;"; 


        $query = $this->db->query($sql, array($id));

        if ($query->num_rows() > 0)
        {
           return $query->row();         
        }
    }

    /**
    * 添加分类
    */
    function Insert($marketname,$province,$city)
    {
        $data = array('marketname' => $marketname,'province' => $province,'city' => $city,'CreateOn' => 'NOW()','Status' => 1,'CreateBy' => 'system','ModifyOn' => 'NOW()' );
        //$sql = "INSERT INTO newscategory (CategoryName,ParentId,Comment,CreateOn,CreateBy,ModifyOn) VALUES (?,?,?,NOW(),?,NOW());";
        $this->load->database();
        $result = $this->db->insert('sm_markets',$data);
        return $result;
    }

    /**
    * 修改分类
    */
    function Update($id,$marketname,$Comment,$parentid)
    {
        $data = array('marketname' => $marketname,'Comment' => $Comment,'ParentId' => $parentid,'ModifyBy' => 'system','ModifyOn' => 'NOW()' );
        $where = array('id' => $id );

        $this->load->database();
        $result = $this->db->update('sm_markets',$data,$where);
        return $result;    
    }

    /**
    * 删除
    */
    function Delete($id)
    {
        $where = array('id' => $id );
        $this->load->database();
        $result = $this->db->delete('sm_markets',$where);
        return $result;
    }
}

?>